package my2Cents.tools;

import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;

import my2Cents.Util;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.PhysShape;

public class ShapeBreaker extends AbstractBodyModifier {
	public ShapeBreaker() {
		setName("Shape Breaker");
		setDescription("Breaks a shape into many\n" +
				"smaller shapes.");
	}
	
	protected void modify(Body b) {
		PhysShape s = b.getShape();
		GeneralPath gp = s.getGeneralPath((Vector2f) b.getPosition(), b.getRotation());
		PathIterator path = gp.getPathIterator(null);
		
		breakPath(path);
		
		b.remove();
	}
	
	private Vector2f coordsToVec(float[] coords) {
		Vector2f out = new Vector2f(coords[0],coords[1]);
		return out;
	}
	
	private void buildLine(float[] c1, float[] c2) {
		Vector2f start = coordsToVec(c1);
		Vector2f end = coordsToVec(c2);
		Body b = Util.makeLine(start,end,2,5);
		this.getWorld().add(b);
	}
	
	private void breakPath(PathIterator path) {
		float[] lastCoord = new float[6];
	    float[] coords = new float[6];
	    for (PathIterator i = path; !i.isDone(); i
	        .next()) {
	      int type = i.currentSegment(coords);
	      switch (type) {
	      case PathIterator.SEG_MOVETO:
	    	  lastCoord[0] = coords[0];
	    	  lastCoord[1] = coords[1];
	        break;
	      case PathIterator.SEG_LINETO:
	    	  if(lastCoord[0] != coords[0] || lastCoord[1] != coords[1]) {
	    		  buildLine(coords,lastCoord);
	    	  }
	    	  lastCoord[0] = coords[0];
	    	  lastCoord[1] = coords[1];
	        break;
	      case PathIterator.SEG_QUADTO:

	        break;
	      case PathIterator.SEG_CUBICTO:

	        break;
	      case PathIterator.SEG_CLOSE:

	        break;
	      }
	    }
		
	}

	protected void unModify(Body b) {

	}
}
